package com.hoshino.momochi.controller;

import com.alibaba.fastjson.JSON;
import com.hoshino.momochi.annotation.IgnoreLogin;
import com.hoshino.momochi.model.domain.UserDetails;
import com.hoshino.momochi.service.FileService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.VditorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class FileController {


    @Autowired
    private FileService fileService;

    /**
     * 图片上传
     *
     * @param files
     * @return
     */
    @RequestMapping("api/file/upload")
    @IgnoreLogin
    public String upload(MultipartFile files) {
        List<String> errFiles = new ArrayList<>();
        Map<String, String> succMap = new HashMap<>();
//        for (MultipartFile file : files) {
//
//        }
        try {
            Map<String, String> map = fileService.upload(files);
            String url = map.get("url");
            System.out.println(files.getOriginalFilename());
            succMap.put(files.getOriginalFilename(), "http://192.168.115.128:8080" + url);
        } catch (IOException e) {
            errFiles.add(files.getOriginalFilename());
            e.printStackTrace();
        }
        VditorResponse vditorResponse = VditorResponse.success(errFiles, succMap);
        return JSON.toJSONString(vditorResponse);
    }


}
